﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Time.IBLL;
using Time.BLL;
using Time.OPCServerV;

namespace MesDock
{
    public partial class frmOPCServerMark : Form
    {
        IEqAlarm_BLL objIEqAlarm_BLL = new EqAlarm_BLL();
        private int item = -1;
        string tempID = null;
        public frmOPCServerMark()
        {
            InitializeComponent();
        }
        Boolean isStation = false;
        private void btnOK_Click(object sender, EventArgs e)
        {
            int result = 0;

            if (!string.IsNullOrEmpty(txtName.Text))
            {
                DataTable objtempDt = null;
                string tempStrSql = null;
                if (isStation)
                {

                    string[] stationD = txtDscription.Text.Split('_');
                    if (stationD.Length < 2)
                    {
                        return;
                    }
                    tempStrSql = string.Format("select * from HK_EqStationCode where 工位 = '{0}' ", stationD[1]);
                    objtempDt = objIEqAlarm_BLL.getData(tempStrSql);

                    if (objtempDt.Rows.Count < 1)
                    {
                        return;
                    }

                    tempStrSql = string.Format("update HK_EqStationCode set {0} = '{1}' where 工位 = '{2}'", stationD[0], txtName.Text, stationD[1]);
                    objIEqAlarm_BLL = new EqAlarm_BLL();
                    result = objIEqAlarm_BLL.ExecuteNonQuery(tempStrSql, null);


                }
                else
                {
                    tempStrSql = string.Format("select * from t_KepServerConfig where item = {0} ", item);

                    objtempDt = objIEqAlarm_BLL.getData(tempStrSql);
                    string[,] option = { { "@Name", txtName.Text }, { "@Dscription", txtDscription.Text } };
                    if (objtempDt.Rows.Count < 1)
                    {
                        tempStrSql = "insert into t_KepServerConfig(Name,Dscription)values(@Name,@Dscription)";
                    }
                    else
                    {
                        tempStrSql = "update t_KepServerConfig set Dscription = @Dscription , Name =@Name where item =" + item;
                    }
                    objIEqAlarm_BLL = new EqAlarm_BLL();
                    result = objIEqAlarm_BLL.ExecuteNonQuery(tempStrSql, option);

                }
            }
            getData();

        }
        private void getData()
        {
            DataTable objDt = objIEqAlarm_BLL.getDataTable("t_KepServerConfig");

            dgvData.DataSource = objDt.DefaultView;
            DataTable objStationPLCAddressDataDt = objIEqAlarm_BLL.getDataTable("HK_EqStationCode");
            dgvStationPLCAddressData.DataSource = objStationPLCAddressDataDt.DefaultView;
            txtName.Text = "";
            txtDscription.Text = "";

        }

        private void frmOPCServerMark_Load(object sender, EventArgs e)
        {
            getData();
        }

        private void dgvData_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            isStation = false;
            try
            {
                txtName.Text = dgvData.CurrentRow.Cells[1].Value.ToString();
                txtDscription.Text = dgvData.CurrentRow.Cells[2].Value.ToString();
                item = Convert.ToInt32(dgvData.CurrentRow.Cells[0].Value.ToString());
                tempID = dgvData.CurrentRow.Cells[3].Value.ToString();
            }
            catch (Exception efe)
            {

            }
        }

        private void dgvStationPLCAddressData_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                isStation = true;
                item = Convert.ToInt32(dgvStationPLCAddressData.CurrentRow.Cells[0].Value.ToString());
               
                txtName.Text = dgvStationPLCAddressData.CurrentCell.Value.ToString();
                txtDscription.Text = dgvStationPLCAddressData.Columns[dgvStationPLCAddressData.CurrentCell.ColumnIndex].HeaderText + "_" + dgvStationPLCAddressData.CurrentRow.Cells[1].Value.ToString();
            }
            catch (Exception exe)
            {

            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {

            string sqlStr = string.Format("delete t_KepServerConfig where ID = '{0}'", tempID);
            objIEqAlarm_BLL = new EqAlarm_BLL();
            objIEqAlarm_BLL.ExecuteNonQuery(sqlStr, null);
                getData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

}
